package com.iot08.admin.mob;

import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.MobNotifyPush;
import com.iot08.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.model.*;
import mob.push.api.model.factory.VivoExtra;

import java.util.*;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2020/12/28 11:06
 */
@Slf4j
public class MyPushWorkBuilder {
    public static final Integer TARGET_ALL = 1;
    public static final Integer TARGET_ALIAS = 2;
    private final Push push = new Push();

    public MyPushWorkBuilder() {
    }

    public Push build() {
        return push;
    }

    /**
     * @param content
     * @time: 2020/11/27 14:42
     * @author: Rock
     * @version 1.0
     */
    public void fillParams(MobNotifyPush notifyPush, String title, String content, String scheme, String data) {
        if (this.push.getPushTarget() == null) {
            this.push.setPushTarget(new PushTarget());
        }
        if (StringUtil.isNotBlank(title)) {
            this.push.getPushNotify().setTitle(title);
        }
        this.push.getPushNotify().setIosProduction(notifyPush.getIosProduction());
        this.push.setWorkno(notifyPush.getId());
        this.push.getPushNotify().setContent(content);
        if (Const.INT_1.equals(notifyPush.getTaskCron())) {
            this.push.getPushNotify().setTaskCron(notifyPush.getTaskCron());
            this.push.getPushNotify().setTaskTime(notifyPush.getTaskTime());
        }
        if (notifyPush.getSpeed() != null && notifyPush.getSpeed() > 0) {
            this.push.getPushNotify().setSpeed(notifyPush.getSpeed());
        }
        if (notifyPush.getOfflineSeconds() != null && notifyPush.getOfflineSeconds() > 0) {
            this.push.getPushNotify().setOfflineSeconds(notifyPush.getOfflineSeconds());
        }

        if (this.push.getPushNotify().getIosNotify() == null) {
            this.push.getPushNotify().setIosNotify(new IosNotify());
        }
        this.push.getPushNotify().getIosNotify().setMutableContent(1);
        this.push.getPushNotify().getIosNotify().setContentAvailable(1);

        if (this.push.getPushFactoryExtra() == null) {
            this.push.setPushFactoryExtra(PushFactoryExtra.builder().build());
        }
        VivoExtra vivoExtra = VivoExtra.builder().build();
        vivoExtra.setClassification(1);
        this.push.getPushFactoryExtra().setVivoExtra(vivoExtra);

        if (this.push.getPushForward() == null) {
            this.push.setPushForward(new PushForward());
        }
        this.push.getPushForward().setScheme(scheme);
        this.push.getPushForward().setNextType(2);

        if (StringUtil.isNotBlank(data)) {
            List<PushMap> schemeDataList = new ArrayList<>();
            PushMap pushMap = new PushMap();
            pushMap.setKey("data");
            pushMap.setValue(data);
            schemeDataList.add(pushMap);
            push.getPushForward().setSchemeDataList(schemeDataList);
        }
    }

    /**
     * 广播设置
     *
     * @param workNo
     * @param title
     * @param content
     * @time: 2020/12/28 11:18
     * @author: Rock
     * @version 1.0
     */
    public MyPushWorkBuilder setTargetAll(MobNotifyPush notifyPush, String title, String content, String scheme, String data) {
        this.fillParams(notifyPush, title, content, scheme, data);
        push.getPushTarget().setTarget(TARGET_ALL);
        return this;
    }

    /**
     * 别名设置
     *
     * @param title
     * @param content
     * @param alias
     * @time: 2020/11/27 14:42
     * @author: Rock
     * @version 1.0
     */
    public MyPushWorkBuilder setTargetByAlias(MobNotifyPush notifyPush, String title, String content, String scheme, String data, Set<String> alias) {
        this.fillParams(notifyPush, title, content, scheme, data);
        push.getPushTarget().setTarget(TARGET_ALIAS);
        push.getPushTarget().setAlias(alias);
        return this;
    }

    /**
     * @param extraMap
     * @time: 2020/11/27 14:31
     * @author: Rock
     * @version 1.0
     */
    public List getExtraParamsList(Map<String, String> extraMap) {
        List<PushMap> pushMapList = new ArrayList<>();
        Iterator<Map.Entry<String, String>> entries = extraMap.entrySet().iterator();
        PushMap pushMap;
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            pushMap = new PushMap();
            pushMap.setKey(entry.getKey());
            pushMap.setValue(entry.getValue());
            pushMapList.add(pushMap);
        }
        return pushMapList;
    }
}
